﻿using System;
using System.Data.SQLite;
using System.Globalization;
using System.IO;
using System.Linq;

namespace GrandExchangeTool
{
    class DatabaseCreator
    {
        private readonly SQLiteConnection _db;

        public DatabaseCreator(string databaseFile, int version)
        {
            databaseFile = "C:/Users/Sven/ItemList.sqlite";
            if (File.Exists(databaseFile)) return;
            SQLiteConnection.CreateFile(databaseFile);
            _db = new SQLiteConnection("Data Source=" + databaseFile + ";Version=" + version.ToString(CultureInfo.InvariantCulture) + ";");
            _db.Open();
            new SQLiteCommand("CREATE TABLE Items (Name varchar(30), id int)", _db).ExecuteNonQuery();
            var list = File.ReadAllLines("ItemList.txt");
            foreach (var line in list.Select(s => s.Replace("'", "#$")).Select(s => s.Split(';')))
            {
                new SQLiteCommand("INSERT INTO Items VALUES ('" + line[0] + "', " + line[1] + ");", _db).ExecuteNonQuery();
                Console.WriteLine("INSERT INTO Items VALUES ('" + line[0] + "', " + line[1] + ");");
            }
            _db.Close();
        }
    }
}
